package com.gking.processHarvest.service.HarvestTags;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.processHarvest.common.BaseContext;
import com.gking.processHarvest.common.CustomException;
import com.gking.processHarvest.common.Res;
import com.gking.processHarvest.entity.HarvestTags;
import com.gking.processHarvest.entity.Tag;
import com.gking.processHarvest.mapper.HarvestTagsMapper;
import com.gking.processHarvest.service.Tag.TagService;
import com.gking.processHarvest.utils.CheckStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)//三级缓存解决bean循环注入问题
public class HarvestTagsServiceImpl extends ServiceImpl<HarvestTagsMapper, HarvestTags> implements HarvestTagsService {

    @Autowired
    private TagService tagService;

    /**
     * 为收获设置标签
     *
     * @param harvestId 收获id
     * @param list      设置的标签列表
     * @return s
     */
    @Override
    public Res<String> set(String harvestId, String[] list) {
        if (harvestId == null || harvestId.equals("")) return Res.error("请将收获id补充完整！");

        Set<String> set = new HashSet<>(Arrays.asList(list));
        if(set.size() != list.length) return Res.error("传入了重复标签！");

        List<HarvestTags> harvestTagsList = new ArrayList<>();

        //校验导入的标签是否正确
        if (list.length != 0) {
            //数组可以使用 Arrays.stream() 方法将其转换为一个 Stream 对象，从而可以使用 map() 方法对数组元素进行映射操作。
            Stream<String> stream = Arrays.stream(list);
            harvestTagsList = stream.map((item) -> {
                CheckStatus<Tag> checkStatus = new CheckStatus<>();
                LambdaQueryWrapper<Tag> queryWrapper = checkStatus.checkIsDelete(
                        item, Tag::getId,
                        Tag::getMasterId, Tag::getIsDelete);
                Tag tag = tagService.getOne(queryWrapper);
                if (tag == null) throw new CustomException("导入的标签id " + item + " 错误！");

                HarvestTags harvestTags = new HarvestTags();
                harvestTags.setHarvestId(harvestId);
                harvestTags.setTagId(tag.getId());
                harvestTags.setName(tag.getName());
                harvestTags.setColor(tag.getColor());
                return harvestTags;
            }).collect(Collectors.toList());
        }

        //先将数据库中的标签列表删除
        LambdaQueryWrapper<HarvestTags> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HarvestTags::getHarvestId, harvestId);
        queryWrapper.eq(HarvestTags::getMasterId, BaseContext.getCurrentId());
        this.remove(queryWrapper);

        //在保存标签数据
        this.saveBatch(harvestTagsList);

        return Res.success("设置成功！");
    }
}
